POST
The tidyquant
package provides tools and data for visualizing and analysing equities.
Here is an example using data from POST.
dat <- tq_get(toString(params$ticker))
dat %>% ggplot(aes(x = date, y = close)) +
geom_barchart(aes(open = open, high = high, low = low, close = close)) +
labs(title = paste(params$ticker,"Bar Chart"), y = "Closing Price", x = "") +
theme_tq()

Or functions of the data, like returns.
dat.new <- dat %>% tq_transmute(select= adjusted,
mutate_fun = periodReturn,
period = "daily",
col_rename = "Ra") %>% as_tsibble(index=date)
dat.new %>% autoplot()

Adding Some Interactives
The following are a few quick interactive plots.
library(tidyquant)
library(tidyverse)
library(magrittr)
# Use tidyquant to get the data
# Slice off the most recent 120 days
dat.tail <- tail(dat, 120)
dat.tail %<>% mutate(
open = round(open, digits=2),
close = round(close, digits=2),
high = round(high, digits=2),
low = round(low, digits=2),
adjusted = round(adjusted, digits=2)
)
Let’s have a look at the data.
library(DT)
datatable(dat.tail)
The Plot
There are a few charts specifically designed for OHLC data that are
included in plotly. Here I want to deploy a basic one with
one modification. I want daily increases in black and daily decreases in
red.
library(plotly)
# basic example of ohlc charts
# custom colors
i <- list(line = list(color = '#000000')) # black
d <- list(line = list(color = '#FF0000')) # red
# Create the figure
fig.2 <- dat.tail %>%
plot_ly(x = ~date, type="ohlc",
open = ~open, close = ~close,
high = ~high, low = ~low,
increasing = i, decreasing = d)
fig.2
ggplot candlestick
dat %>%
ggplot(aes(x = date, y = close)) +
geom_candlestick(aes(open = open, high = high, low = low, close = close)) +
labs(title = paste0(params$ticker," Candlestick Chart", sep=""), y = "Closing Price", x = "") +
theme_tq()

Another Plot
dat %>%
ggplot(aes(x = date, y = close)) +
geom_barchart(aes(open = open, high = high, low = low, close = close)) +
geom_ma(ma_fun = EMA, n = 50, wilder = TRUE, linetype = 5, size = 1.25) +
geom_ma(ma_fun = EMA, n = 200, wilder = TRUE, color = "red", size = 1.25) +
labs(title = paste0(dat$symbol," Bar Chart", sep=""),
subtitle = "50 and 200-Day EMA",
y = "Closing Price", x = "") +
coord_x_date() +
theme_tq()

Bache, Stefan Milton, and Hadley Wickham. 2022.
Magrittr: A
Forward-Pipe Operator for r.
https://CRAN.R-project.org/package=magrittr.
Dancho, Matt, and Davis Vaughan. 2022.
Tidyquant: Tidy Quantitative
Financial Analysis.
https://github.com/business-science/tidyquant.
Grolemund, Garrett, and Hadley Wickham. 2011.
“Dates and Times
Made Easy with lubridate.”
Journal of Statistical Software 40 (3): 1–25.
https://www.jstatsoft.org/v40/i03/.
Müller, Kirill, and Hadley Wickham. 2022.
Tibble: Simple Data
Frames.
https://CRAN.R-project.org/package=tibble.
Ryan, Jeffrey A., and Joshua M. Ulrich. 2022a.
Quantmod:
Quantitative Financial Modelling Framework.
https://CRAN.R-project.org/package=quantmod.
———. 2022b.
Xts: eXtensible Time Series.
https://github.com/joshuaulrich/xts.
Spinu, Vitalie, Garrett Grolemund, and Hadley Wickham. 2023.
Lubridate: Make Dealing with Dates a Little Easier.
https://CRAN.R-project.org/package=lubridate.
Ulrich, Joshua. 2021.
TTR: Technical Trading Rules.
https://github.com/joshuaulrich/TTR.
Wickham, Hadley. 2016.
Ggplot2: Elegant Graphics for Data
Analysis. Springer-Verlag New York.
https://ggplot2.tidyverse.org.
———. 2022a.
Stringr: Simple, Consistent Wrappers for Common String
Operations.
https://CRAN.R-project.org/package=stringr.
———. 2022b.
Tidyverse: Easily Install and Load the Tidyverse.
https://CRAN.R-project.org/package=tidyverse.
———. 2023.
Forcats: Tools for Working with Categorical Variables
(Factors).
https://CRAN.R-project.org/package=forcats.
Wickham, Hadley, Mara Averick, Jennifer Bryan, Winston Chang, Lucy
D’Agostino McGowan, Romain François, Garrett Grolemund, et al. 2019.
“Welcome to the tidyverse.”
Journal of Open Source Software 4 (43): 1686.
https://doi.org/10.21105/joss.01686.
Wickham, Hadley, Winston Chang, Lionel Henry, Thomas Lin Pedersen,
Kohske Takahashi, Claus Wilke, Kara Woo, Hiroaki Yutani, and Dewey
Dunnington. 2023.
Ggplot2: Create Elegant Data Visualisations Using
the Grammar of Graphics.
https://CRAN.R-project.org/package=ggplot2.
Wickham, Hadley, Romain François, Lionel Henry, Kirill Müller, and Davis
Vaughan. 2023.
Dplyr: A Grammar of Data Manipulation.
https://CRAN.R-project.org/package=dplyr.
Wickham, Hadley, and Lionel Henry. 2023.
Purrr: Functional
Programming Tools.
https://CRAN.R-project.org/package=purrr.
Wickham, Hadley, Jim Hester, and Jennifer Bryan. 2023.
Readr: Read
Rectangular Text Data.
https://CRAN.R-project.org/package=readr.
Wickham, Hadley, Davis Vaughan, and Maximilian Girlich. 2023.
Tidyr:
Tidy Messy Data.
https://CRAN.R-project.org/package=tidyr.
Xie, Yihui, Joe Cheng, and Xianying Tan. 2023.
DT: A Wrapper of the
JavaScript Library DataTables.
https://github.com/rstudio/DT.
Zeileis, Achim, and Gabor Grothendieck. 2005.
“Zoo: S3
Infrastructure for Regular and Irregular Time Series.”
Journal of Statistical Software 14 (6): 1–27.
https://doi.org/10.18637/jss.v014.i06.
Zeileis, Achim, Gabor Grothendieck, and Jeffrey A. Ryan. 2022.
Zoo:
S3 Infrastructure for Regular and Irregular Time Series (z’s Ordered
Observations).
https://zoo.R-Forge.R-project.org/.